<?php

namespace App\Controllers\Diary;


use App\Models\Goods\Goods;
use App\Models\Diary\Topic;
use App\Models\Diary\Diary;
use App\Models\Store\Store;
use App\Models\Goods\GoodsAffiliate;
use App\Libs\Helper\Util;
use App\Models\Diary\Comment;
use App\Models\User\User;
use App\Libs\Helper\CommonHelper;

class GetDiaryDetail extends \App\Libs\Base\BaseAction
{


    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = false;
    }

    /**
     * @return array 根据传入的diaryid，返回日记的详细信息；
     * 可选传入：用户的经纬度，返回用户距离目标商铺的距离；可以定制一起返回的评论的数量；
     * diary=4&latitude=20.5&longitude=23.6&cnum=5
     */
    public function execute()
    {
        $diaryId = $this->request->get('diary', 'int', 0);
        if ($diaryId == 0) {
            return \App\Libs\Views\Json::view(1, '缺少关键参数diary', null);
        }
        $matchedDiary = Diary::find([
            'conditions' => 'id=?1',
            'columns' => 'id,gid,eid,diary_logo,diary_name,editor_name,period,diary_num,ctime,diary_content,isshow,read_num,zan_num',
            'bind' => [1 => $diaryId],
            //需要处理的数据：period,ctime,
        ]);
        if (empty($matchedDiary)) {
            return \App\Libs\Views\Json::view(1, '指定的日记不存在', null);
        }
        if ($matchedDiary->isshow == false) {
            return \App\Libs\Views\Json::view(1, '指定的日记暂不允许显示', null);
        }
        //增加读数
        Diary::addReadNum($matchedDiary->id);



        //todo：转换期数为"最新"或者"第几期":缩减为一个方法，或者函数，重用
        $topicModel = new Topic();
        $period = $topicModel->getNewlestPeriodNum();
        if ($matchedDiary->period == $period) {
            $matchedDiary->period = '最新';
        } else {
            $matchedDiary->period = '第' . $matchedDiary->period . '期';
        }

        $goodsId = $matchedDiary->gid;
        unset($matchedDiary->gid);
        $finalDiaryDetailData = new \stdClass();
        $finalDiaryDetailData->diaryData = $matchedDiary;
        //主要  商品的相关信息，
        $recomGoods = Goods::find([
            'conditions' => 'id = ?1',
            'columns' => 'id,sid,goods_name,goods_img,goods_price,sale_price',
            'bind' => [1 => $goodsId]
        ]);
        if (!empty($recomGoods)) {
            //附属的商品相关信息
            $goodsAttroduce = GoodsAffiliate::find([
                'conditions' => 'gid = ?1',
                'columns' => 'introduce',
                'bind' => [1 => $goodsId],
            ])->introduce;
            if (empty($goodsAttroduce)) {
                $recomGoods->introduce = null;
            } else {
                $recomGoods->introduce = $goodsAttroduce;
            }
            //去店铺获得信息；
            $salerId = $recomGoods->sid;
            unset($recomGoods->sid);
            $matchedStore = Store::getStoreInfo($salerId);
            if (empty($matchedStore)) {
                $recomGoods->address = null;
            } else {
                $recomGoods->address = $matchedStore->store_address;
            }
            //获得距离
            if ($this->request->has('longitude') && $this->request->has('latitude')) {
                $distance = CommonHelper::getDistance($this->request->get('latitude'), $this->request->get('longitude'), $matchedStore->latitude, $matchedStore->longitude);
                $recomGoods->distance = $distance;
            }
            $recomGoods->goods_price = Goods::formatPrice($recomGoods->goods_price);
            $recomGoods->sale_price = Goods::formatPrice($recomGoods->sale_price);
            $finalDiaryDetailData->recGoods = $recomGoods;
        }

        //获取部分评论
        $commentNum = $this->request->get('cnum', 'int', 5);
        $cmtData = Comment::getCmt($diaryId, 1, $commentNum);

        $finalDiaryDetailData->comment = $cmtData;
        return \App\Libs\Views\Json::view(0, '成功', $finalDiaryDetailData);
    }
}